package com.util.core;

import javax.servlet.ServletContext;

import org.springframework.beans.BeansException;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class BusinessFactory {
	
	public static BusinessObject createBusiness(){
		return new BusinessObject();
	}
	
	public static Business createBusiness(ServletContext servletContext,String className){
		WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
		Business biz = null;
		try{
			biz = (Business) context.getBean(className);
		}catch(BeansException beanEx){
			biz = createBusiness(className);
		}
		if(biz == null){
			try {
				throw new Exception("类名: "+className+"无法找到");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return biz;
	}
	
	public static Business createBusiness(String className){
		Class<?> cls = null;
		try {
			cls = Class.forName(className);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		Business biz = null;
		try {
			biz = (Business)cls.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		
		return biz;
	}
}
